<?php
class DataUserGroups{
	private $Groups = array();
	function __construct(){
	}
	static function swap($bytebuffer){
		$obj = new DataUserGroups();
		$num_of_group = DataUtil::readInt32( $bytebuffer ) ;
		for($i = 0; $i < $num_of_group; $i++){
			$obj->Groups[] = DataUtil::readBBin( $bytebuffer ) ;
		}
		return $obj;
	}
	function addGroup($group_name){
		if(is_array($this->Groups)){
			foreach($this->Groups as $group_bytes){
				if(implode(array_map("chr",  $group_bytes ))  == $group_name){
					return;
				}
			}
		}
		$this->Groups[] = unpack('C*', $group_name ) ;
	}
	function getGroups(){
		$rs = array();
		if(is_array($this->Groups)){
			foreach($this->Groups as $group_bytes){
				$rs[] = implode(array_map("chr",  $group_bytes )) ;
			}
		}
		return $rs;
	}
	function getBinSize(){
		$size = 0;
		$size += 4 ;
		if(is_array($this->Groups)){
			foreach($this->Groups as $group_name){
				$size += 1  + count($group_name);
			}
		}
		return $size;
	}
	function getBinary(){
		$buf = ByteBuffer::allocate( $this->getBinSize() ) ;
		DataUtil::writeInt32( $buf ,  count($this->Groups) ) ;
		if(is_array($this->Groups)){
			foreach($this->Groups as $group_name){
				DataUtil::writeBBin( $buf , $group_name ) ;
			}
		}
		return  $buf ->_array() ;
	}
}
?>